using System;
using System.Drawing;

namespace EternitySolver.Core
{
    public static class BoardRenderer
    {
        public static Bitmap GetBitmap(this Board board, int width, int height)
        {
            var bitmap = new Bitmap(width, height);
            var graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.Black);
            int position = -1;
            int widthstep = width / board.Width;
            int heightstep = height / board.Height;
            int halfwidthstep = (width / board.Width) / 2;
            int halfheightstep = (height / board.Height) / 2;
            foreach (Tile tile in board.PlacedTiles)
            {
                position++;
                if (tile == null) continue;

                int leftoffset = (position % board.Width) * widthstep;
                int topoffset = (int)Math.Floor(position / (double)board.Width) * heightstep;

                var centrepoint = new Point(leftoffset + halfwidthstep, topoffset + halfheightstep);
                var pointA = new Point(leftoffset, topoffset);
                var pointB = new Point(leftoffset + widthstep, topoffset);
                var pointC = new Point(leftoffset + widthstep, topoffset + heightstep);
                var pointD = new Point(leftoffset, topoffset + heightstep);

                graphics.FillPolygon(IntegerToBrush(tile.Top), new[] { pointA, pointB, centrepoint });
                graphics.FillPolygon(IntegerToBrush(tile.Right), new[] { pointB, pointC, centrepoint });
                graphics.FillPolygon(IntegerToBrush(tile.Bottom), new[] { pointC, pointD, centrepoint });
                graphics.FillPolygon(IntegerToBrush(tile.Left), new[] { pointA, centrepoint, pointD });

                graphics.DrawRectangle(Pens.Black, leftoffset, topoffset, widthstep, heightstep);
            }

            graphics.Dispose();
            return bitmap;
        }

        private static Brush IntegerToBrush(int color)
        {
            switch (color)
            {
                case 1: return Brushes.Red;
                case 2: return Brushes.Green;
                case 3: return Brushes.Blue;
                case 4: return Brushes.Yellow;
                case 5: return Brushes.Cyan;
                case 6: return Brushes.Magenta;
                case 7: return Brushes.LightGray;
                case 8: return Brushes.Purple;
                case 9: return Brushes.Turquoise;
                case 10: return Brushes.Orange;
                case 11: return Brushes.Pink;
                case 12: return Brushes.Brown;
                case 13: return Brushes.LightBlue;
                case 14: return Brushes.BlueViolet;
                case 15: return Brushes.Crimson;
                case 16: return Brushes.Lavender;
                case 17: return Brushes.BlueViolet;
                case 18: return Brushes.Chartreuse;
                case 19: return Brushes.Teal;
                case 20: return Brushes.Thistle;
                case 21: return Brushes.MintCream;
                case 22: return Brushes.Firebrick;
                case 23: return Brushes.LemonChiffon;
                case 24: return Brushes.HotPink;
                case 25: return Brushes.Ivory;
                case 26: return Brushes.LightBlue;
                default: return Brushes.Black;
            }
        }        
    }
}